﻿/**
 * Reboulder - Partial Boulder Dash clone
 * Jesús Sagra, 2014
 * 
 * Game class: game logic
 * 
 * Version    Date     Changes
 * -----------------------------------------------------
 *  0.01   06-mar-2015  Methods: Run(skeleton), DrawElements, CheckInput, 
 *                               WaitForNextFrame. 
 *  0.02   06-mar-2015  Check collisons with background at CheckInput.
 *  0.03   06-mar-2015  Pick items and clear sand gives points. Clear picked item.
 *                      Clear sand when player crosses it.                  
 */

class Game
{
    protected bool finished;
    protected Player player1;
    protected Map myMap;
    protected ScoreBar scores;

    public Game()
    {
        finished = false;
        myMap = new Map("level2");
        player1 = myMap.GetPlayer();
        scores = new ScoreBar(myMap.GetAmountOfItems(), player1.Lives);
    }


    public void Run()
    {
        //int levelNumber = 1;//Will change after complete a level
        //myMap = new Map("level" + levelNumber);
        // Game Loop
        do
        {
            // Update screen
            DrawElements();

            // Check input by the user
            CheckInput();

            // Move enemies, background, etc 
            MoveElements();

            // Check collisions and apply game logic
            CheckCollisions();

            // Pause till next frame
            WaitForNextFrame();
        }
        while (!finished);
    }


    public void DrawElements()
    {
        SdlHardware.ClearScreen();
        
        // Map
        myMap.Draw();
        // Player
        player1.DrawOnHiddenScreen();
        // Scorebar
        scores.Draw();
        
        SdlHardware.ShowHiddenScreen();
    }


    public void CheckInput()
    {
        if (SdlHardware.KeyPressed(SdlHardware.KEY_RIGHT) && myMap.CanMoveTo(
            player1.X + player1.Speed,
            player1.Y,
            player1.X + player1.Speed + player1.Width,
            player1.Y + player1.Height))
        {
            player1.MoveRight();
            Scroll();
        }
        else if (SdlHardware.KeyPressed(SdlHardware.KEY_LEFT) && myMap.CanMoveTo(
            player1.X - player1.Speed,
            player1.Y,
            player1.X + player1.Width - player1.Speed,
            player1.Y + player1.Height))
        {
            player1.MoveLeft();
            Scroll();
        }
        else if (SdlHardware.KeyPressed(SdlHardware.KEY_DOWN) && myMap.CanMoveTo(
            player1.X,
            player1.Y + player1.Speed,
            player1.X + player1.Width,
            player1.Y + player1.Speed + player1.Height))
        {
            player1.MoveDown();
            Scroll();
        }
        else if (SdlHardware.KeyPressed(SdlHardware.KEY_UP) && myMap.CanMoveTo(
            player1.X,
            player1.Y - player1.Speed,
            player1.X + player1.Width,
            player1.Y + player1.Height - player1.Speed))
        {
            player1.MoveUp();
            Scroll();
        }
        //else if (SdlHardware.KeyPressed(SdlHardware.KEY_RIGHT) &&
        //    SdlHardware.KeyPressed(SdlHardware.KEY_SPC))
        //    {
        //        if(myMap.PushRight())
        //            player1.MoveRight();
        //    }
        //else if (SdlHardware.KeyPressed(SdlHardware.KEY_LEFT) &&
        //    SdlHardware.KeyPressed(SdlHardware.KEY_SPC))
        //    {
        //        if (myMap.PushLeft())
        //            player1.MoveLeft();no hay k mover el payer,mirar video
        //    }
        else if (SdlHardware.KeyPressed(SdlHardware.KEY_ESC))
            finished = true;
    }


    public void MoveElements()
    {
        myMap.MoveRoundObjects();
    }


    public void CheckCollisions()
    {
        // Door
        // If door is open, player can get through it and finishes the level
        if (player1.CollisionsWith(myMap.GetDoor()) && 
            myMap.GetDoor().IsOpen())
        {
            finished = true;
        }

        //Item
        // If item is picked, gives 100pts and hides
        // Door opens when all objects are taken
        if (myMap.GetCollisions() == "Item")
        {
            scores.SetScore(100);
            scores.SetPickedItems();

            if (scores.GetPickedItems() == 2)
            {
                myMap.GetDoor().Open();
            }
        }

        // Rocks
        // If rock collides with player dies
        if (myMap.GetCollisions() == "Rock")
        {
            player1.Die();
            scores.SetLives(player1.Lives);
            if (player1.Lives == 0)
            {
                finished = true;
            }
        }
    }


    // Pause till next frame (40 ms = 25 fps)
    public static void WaitForNextFrame()
    {
        SdlHardware.Pause(80);
    }


    public void Scroll()
    {
        //si hay 10 o menos casillas por la dereha el scroll se desplazara
        if (player1.ScreenX > 672 &&
            //si me acerco al borde del mapa(ancho del mapa menos 10 casillas) el scroll se para
            player1.X < (myMap.MapWidth - 10) * myMap.BackgroundTileSize)
        {
            //mueve el scrool de sdl 32 a la drcha
            SdlHardware.ScrollHorizontally((short)-player1.Speed);
            //corrige la posicion en pantalla del player para detener el scroll
            player1.ScreenX -= player1.Speed;
        }
        //10casilla por la izq
        else if (player1.ScreenX < 320 && 
            player1.X > 9 * myMap.BackgroundTileSize)
        {
            SdlHardware.ScrollHorizontally((short)+player1.Speed);
            player1.ScreenX += player1.Speed;
        }
        //7casillas por arriba y abajo
        else if (player1.ScreenY > 512 &&
            player1.Y < (myMap.MapHeight - 6) * myMap.BackgroundTileSize)
        {
            SdlHardware.ScrollVertically((short)-player1.Speed);
            player1.ScreenY -= player1.Speed;
        }
        else if (player1.ScreenY < 224 &&
            player1.Y > 6 * myMap.BackgroundTileSize)
        {
            SdlHardware.ScrollVertically((short)player1.Speed);
            player1.ScreenY += player1.Speed;
        }
    }
}